package com.jcfk.eam.controller.bd;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.enums.DeleteFlag;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.business.utils.DataBindHelper;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import com.jcfk.eam.domain.dto.base.EamAssetTypeDTO;
import com.jcfk.eam.domain.dto.base.EamFaultModeAssetDTO;
import com.jcfk.eam.domain.dto.base.EamFaultModeDTO;
import com.jcfk.eam.domain.po.base.EamFaultModeAsset;
import com.jcfk.eam.domain.query.base.EamAssetTypeQuery;
import com.jcfk.eam.domain.query.base.EamFaultModeAssetQuery;
import com.jcfk.eam.domain.query.base.EamFaultModeQuery;
import com.jcfk.eam.enums.AssetKind;
import com.jcfk.eam.service.base.EamAssetTypeService;
import com.jcfk.eam.service.base.EamFaultModeAssetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.jcfk.common.core.utils.ExcelUtils;

import java.util.List;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 故障模式资产
 *
 * @author yrl
 * @email 
 * @date 2024-12-02
 */
@Api(tags = {"故障模式资产管理"})
@RestController
@RequestMapping("eamfaultmodeasset")
public class EamFaultModeAssetController {
    @Autowired
    private EamFaultModeAssetService eamFaultModeAssetService;

    @Autowired
    private EamAssetTypeService eamAssetTypeService;
    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamFaultModeAssetQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询故障模式资产")
    @PostMapping("/queryByPage")
    public RetPage<EamFaultModeAssetDTO> queryByPage(@RequestBody ReqPageParam<EamFaultModeAssetQuery> requestParam) {
        DataPage<EamFaultModeAssetDTO> page = new DataPage<EamFaultModeAssetDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamFaultModeAssetDTO.class);
        page = eamFaultModeAssetService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     *  故障模式资产
     * 根据id 查询详情
     */
    @ApiOperation("查询故障模式资产")
    @GetMapping("/get/{id}")
    public RetResult<EamFaultModeAssetDTO> get(@PathVariable("id") String id) {
            EamFaultModeAssetDTO item = eamFaultModeAssetService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理故障模式资产
     */
    @ApiOperation("保存故障模式资产")
    @PostMapping("/save")
    @RequestLogHeader(modular = "故障模式资产管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamFaultModeAssetDTO eamFaultModeAsset) {
        // 数据校验
        RetResult ret = eamFaultModeAssetService.isValid(eamFaultModeAsset);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamFaultModeAsset.getId())){
            // 新增
            eamFaultModeAssetService.insert(eamFaultModeAsset);
        } else{
            // 更新
            eamFaultModeAssetService.update(eamFaultModeAsset);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除故障模式资产
     */
    @ApiOperation("批量删除故障模式资产")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "故障模式资产管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamFaultModeAssetDTO eamFaultModeAsset) {
        return RetResult.ok(eamFaultModeAssetService.logicDeleteList(eamFaultModeAsset));
    }





    /**
     *  删除故障模式资产
     */
    @ApiOperation("删除故障模式资产")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "故障模式资产管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamFaultModeAssetService.deleteItem(id);
        return RetResult.ok();
    }


    /**
     * 获取可用列表
     * @param query
     * @return
     */
    @PostMapping("/getList")
    public RetResult<List<EamFaultModeAssetDTO>> getAvailableList(@RequestBody EamFaultModeAssetQuery query) {
        if (query.getEnabled() == null) {
            query.setEnabled(AvailableStatus.ENABLE.getValue());
        }
        if (query.getDeleted() == null)
            query.setDeleted(DeleteFlag.NOTDELETED.getValue());
        List<EamFaultModeAssetDTO> list = eamFaultModeAssetService.getList(query);
        return RetResult.ok(list);
    }



    /**
     * 添加/更新管理故障模式资产
     */
    @ApiOperation("保存故障模式资产")
    @PostMapping("/saveBatch")
    @RequestLogHeader(modular = "故障模式资产管理", businessType = "保存")
    public RetResult<?> saveBatch(@RequestBody @Valid List<EamFaultModeAssetDTO> eamFaultModeAsset) {
        Assert.isTrue(CollUtil.isNotEmpty(eamFaultModeAsset), "保存的故障模式资产不能为空");
        eamFaultModeAssetService.insertBatch(eamFaultModeAsset);
        return RetResult.ok();
    }


}
